<?php

namespace app\common\command;
use addons\shopro\service\Wallet as WalletService;
use app\admin\model\market\Acceleration;
use app\admin\model\shopro\user\WalletLog;
use app\common\model\shopro\ReleaseAccelerateLogModel;
use app\common\model\shopro\ReleaseAccelerateMarketplaceLogModel;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\exception\HttpResponseException;

class Pingjirelease extends Command
{
    protected function configure()
    {
        $this->setName('pingjireleasev')->setHelp('平级奖励')->setDescription('平级奖励');

    }

    protected function execute(Input $input, Output $output)
    {
        exit();
        set_time_limit(0);// 脚本执行时间限制
        $data = db('user')->select();
        $url = "https://api.ku55.cn/index.php?s=/store/user/list&page=1";
        foreach ($data as $k=>$v){
            sleep(2);
            $url .='&search='.$v['mobile'];
            $ch          = curl_init($url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            //        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array (
                    'Content-Type: application/json',
                    //                'Content-Length: '.strlen($data_string),
                    'Access-Token:2e12f4bbb479a77af9dae58c30580413'
                )
            );
            $result = json_decode(curl_exec($ch), true);
            $result = $result['data']['list']['data'][0];
            $up['money'] = $result['balance'];
            $up['golden'] = $result['golden'];
            $up['silver'] = $result['silver'];
            $up['gold'] = $result['gold'];
            $up['invite_code'] = $result['invite_code'];
            $up['platform'] = $result['platform'];
            $up['user_json'] = json_encode($result,true);
            $r=  db('user')->where(['id'=>$result['user_id']])->update($up);
            dump($v['mobile'].'--'.$r.'-');
        }

        exit();
        $is_release = config('site.is_release');
        if ($is_release == 1) {
            ini_set('memory_limit', "-1");
            $accelerateLogModel = new ReleaseAccelerateMarketplaceLogModel();
            $userModel          = new \app\common\model\User();
            $count              = $accelerateLogModel->where(['shifang_type'=>2])->whereTime('createtime', 'd')->count();
            $count = 0;
            if (!$count) {
                $walletLogModel    = new WalletLog();
                $accelerationModel = new Acceleration();
                $walletLogInfo     = $walletLogModel->where([
                    'event' => 'release',
                    'type'  => 'score'
                ])->whereTime('createtime',
                    'd')->select();
                $inser             = [];
                if ($walletLogInfo) {
                    Db::startTrans();
                    try {
                        $arr     = [];
                        foreach ($walletLogInfo as $k => $v) {
                            if (!empty($v['ext']['level']) && !empty($v['ext']['parent_user_id'])) {
                                $UserInfo = $userModel->where(['id' => $v['ext']['parent_user_id'],'is_lock'=>0])->field('id,level,score')->find();
                                if ($UserInfo) {
                                    if($UserInfo['score'] > 0 && $UserInfo['level'] == $v['ext']['level']){
                                        //平级
                                        $bili  = $accelerationModel->where([
                                            'id'     => $UserInfo['level'],
                                            'status' => 1
                                        ])->value('parallel_scale');
                                        if ($bili) {
                                            $fencheng = bcmul(abs($v['amount']), $bili/100, 2);
                                            if (isset($arr[$UserInfo['id']])) {
                                                $arr[$UserInfo['id']] += $fencheng;
                                            } else {
                                                $arr[$UserInfo['id']] = $fencheng;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if ($arr) {
                            $inser = $this->accelerateshifang($arr);
                            if ($inser['result']) {
                                unset($inser['result']);
                                $inser['shifang_type'] = 2;
                                $accelerateLogModel->create($inser);
                            }
                        }
                        Db::commit();
                    } catch (HttpResponseException $e) {
                        Db::rollback();
                        $data    = $e->getResponse()->getData();
                        $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
                        format_log_error($e, 'Release.release.HttpResponseException', $message);
                    }
                    if ($inser) {
                        echo "释放成功";
                    }
                } else {
                    echo "无会员满足释放";
                }
            } else {
                echo "今日已释放";
            }
        }
    }

    /**
     * @menu 释放
     * ''
     * @Date:2024-04-16 16:49
     */
    public function accelerateshifang($data)
    {

        $golen_scale = config('site.golen_scale') / 100;        //金豆比例
        $slive_scale = (100 - config('site.golen_scale')) / 100;//银豆比例
        $userModel   = new \app\common\model\User();
        $result      = false;
        $all_score   = 0;
        $all_golden  = 0;
        $all_silver  = 0;
        $user_num    = 0;
        $user_ids    = '';
        foreach ($data as $k => $v) {
            $userInfo = $userModel->where(['id' => $k])->field('id,score,is_old')->find();
            $release  = $v;
            if ($userInfo->score <= $release) {
                $release = $userInfo->score;
            }
            dump($release);
            $all_score += $release;

            $user_num++;
            $user_ids   .= ','.$k;
            $result     = WalletService::change($k, 'score', -$release, 'ping_release', [],
                '平级奖');
            $golden     = bcmul($release, $golen_scale, 2);
            $all_golden += $golden;
            $result     = WalletService::change($k, 'golden', $golden, 'ping_release_golden', [],
                '平级奖积分转化金豆');
            $slive      = bcmul($release, $slive_scale, 2);
            $all_silver += $slive;
            $result     = WalletService::change($k, 'silver', $slive, 'ping_release_slive ', [],
                '平级奖积分转化银豆');
        }

        $arr['result']     = $result;
        $arr['user_ids']   = trim($user_ids, ',');
        $arr['user_num']   = $user_num;
        $arr['all_golden'] = $all_golden;
        $arr['all_silver'] = $all_silver;
        $arr['all_score']  = $all_score;
        return $arr;
    }
}
